package com.xique.park.controller.client;

import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.page.TableDataInfo;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.park.bean.domain.client.ParkClientUser;
import com.xique.park.bean.request.client.ClientUserAddRequest;
import com.xique.park.bean.request.client.ClientUserEditRequest;
import com.xique.park.bean.request.client.ClientUserListRequest;
import com.xique.park.service.biz.service.IParkClientUserBizService;
import com.xique.park.service.service.IParkClientUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 岗亭用户Controller
 *
 * @author caogq
 * @date 2021-07-20
 */
@RestController
@RequestMapping("/clientUser")
public class ParkClientUserController extends BaseController {

    @Autowired
    private IParkClientUserService parkClientUserService;

    @Autowired
    private IParkClientUserBizService parkClientUserBizService;

    /**
     * 查询岗亭用户列表
     */
    @PreAuthorize(hasPermi = "park:clientUser:list")
    @PostMapping("/list/{parkId}")
    public TableDataInfo list(ParkClientUser parkClientUser) {
        startPage();
        List<ParkClientUser> list = parkClientUserService.selectParkClientUserList(parkClientUser);
        return getDataTable(list);
    }

    /**
     * 新增保存岗亭用户
     */
    @PreAuthorize(hasPermi = "park:clientUser:add")
    @Log(title = "岗亭用户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(ParkClientUser parkClientUser) {
        ParkClientUser search = new ParkClientUser();
        search.setLoginName(parkClientUser.getLoginName());
        List<ParkClientUser> parkClientUsers = parkClientUserService.selectParkClientUserList(search);
        if (!parkClientUsers.isEmpty()) {
            return AjaxResult.error("账号已存在，请重新输入！");
        }
        return parkClientUserBizService.saveParkClientUser(parkClientUser);
    }

    /**
     * 修改保存岗亭用户
     */
    @PreAuthorize(hasPermi = "park:clientUser:edit")
    @Log(title = "岗亭用户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult editSave(ParkClientUser parkClientUser) {
        return parkClientUserBizService.updateParkClientUser(parkClientUser);
    }

    /**
     * 删除岗亭用户
     */
    @PreAuthorize(hasPermi = "park:clientUser:remove")
    @Log(title = "岗亭用户", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(String ids) {
        int i = parkClientUserBizService.deleteParkClientUserByIds(ids);
        return toAjax(i);
    }

    @PreAuthorize(hasPermi = "park:clientUser:resetPwd")
    @Log(title = "重置密码", businessType = BusinessType.UPDATE)
    @PostMapping("/resetPwd")
    public AjaxResult resetPwdSave(ParkClientUser parkClientUser) {
        //TODO 待处理
        parkClientUserService.updateParkClientUser(parkClientUser);
        return success();
    }

    /* =====================================================岗亭用户V1====================================================== */

    /**
     * 查询岗亭用户列表
     */
    @PreAuthorize(hasPermi = "park:clientUser:list")
    @GetMapping("/v1/list")
    public AjaxResult listV1(ClientUserListRequest request) {
        request.setParkId(SecurityUtils.getParkId());
        startPage();
        return parkClientUserBizService.getClientUserList(request);
    }

    @PreAuthorize(hasPermi = "park:clientUser:add")
    @Log(title = "岗亭用户", businessType = BusinessType.INSERT)
    @PostMapping("/v1/add")
    public AjaxResult addV1(@RequestBody @Valid ClientUserAddRequest request) {
        request.setParkId(SecurityUtils.getParkId());
        return parkClientUserBizService.addClientUser(request);
    }

    @PreAuthorize(hasPermi = "park:clientUser:edit")
    @Log(title = "岗亭用户", businessType = BusinessType.UPDATE)
    @PostMapping("/v1/edit")
    public AjaxResult editV1(@RequestBody @Valid ClientUserEditRequest request) {
        request.setParkId(SecurityUtils.getParkId());
        return parkClientUserBizService.editClientUser(request);
    }

    @PreAuthorize(hasPermi = "park:clientUser:list")
    @GetMapping("/v1/detail")
    public AjaxResult detailV1(Long userId) {
        return parkClientUserBizService.getClientUserDetail(userId);
    }

    @PreAuthorize(hasPermi = "park:clientUser:remove")
    @Log(title = "岗亭用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/v1/remove")
    public AjaxResult removeV1(Long userId) {
        return parkClientUserBizService.removeClientUser(userId);
    }

    @PreAuthorize(hasPermi = "park:clientUser:resetPwd")
    @Log(title = "重置密码", businessType = BusinessType.UPDATE)
    @PostMapping("/v1/resetPwd")
    public AjaxResult resetPwdV1(Long userId) {
        return parkClientUserBizService.resetClientUserPwd(userId);
    }

    /**
     * 获取岗亭用户是否拥有移动端车辆管理权限
     */
    @Log(title = "获取岗亭用户是否拥有移动端车辆管理权限", businessType = BusinessType.OTHER)
    @GetMapping("/getCarAuth/{parkClientUserId}")
    public AjaxResult getCarAuth(@PathVariable("parkClientUserId") Long parkClientUserId) {
        ParkClientUser parkClientUser = parkClientUserBizService.getClientUserById(parkClientUserId);
        if (parkClientUser != null){
            return toAjaxData(parkClientUser.getCarAuth());
        }
        return error("用户不存在");
    }
}


